const formatTime = (date) => {
    const year = date.getFullYear();
    const month = date.getMonth() + 1;
    const day = date.getDate();
    const hour = date.getHours();
    const minute = date.getMinutes();
    const second = date.getSeconds();
    return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}`;
};
const formatNumber = (n) => {
    n = n.toString();
    return n[1] ? n : `0${n}`;
};
const formatDayTime = (val) => {
    if (val) {
        let date = new Date(val);
        let Y = date.getFullYear();
        let M = date.getMonth() + 1;
        let D = date.getDate();
        if (M < 10) {
            M = '0' + M;
        }
        if (D < 10) {
            D = '0' + D;
        }
        return Y + '-' + M + '-' + D;
    } else {
        return '';
    }
};
const isDuringDate = (beginDateStr, endDateStr) => {
    var curDate = new Date();
    var beginDate = new Date(beginDateStr);
    var endDate = new Date(endDateStr);
    if (curDate >= beginDate && curDate <= endDate) {
        return true;
    }
    return false;
};

//根据当前时间比较后端返回的时间计算时间差 转化为秒
const calculateTimeDifferenceInSeconds = (startTime, endTime) => {
    const start = new Date(startTime).getTime();
    const end = new Date(endTime).getTime();
    const difference = Math.abs(end - start);
    const seconds = Math.floor(difference / 1000);
    return seconds;
};
//获取当前时间

const getCurrentDateTime = () => {
    const now = new Date();
    const year = now.getFullYear();
    const month = String(now.getMonth() + 1).padStart(2, '0');
    const day = String(now.getDate()).padStart(2, '0');
    const hours = String(now.getHours()).padStart(2, '0');
    const minutes = String(now.getMinutes()).padStart(2, '0');
    const seconds = String(now.getSeconds()).padStart(2, '0');
    const dateTime = `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
    return dateTime;
};
const updateCountdown = (deadline) => {
    const now = new Date();
    const remainingTime = deadline - now;
    if (remainingTime <= 0) {
        return 0;
    }
    const days = Math.floor(remainingTime / (3600000 * 24));
    const hours = Math.floor((remainingTime % (3600000 * 24)) / 3600000);
    const minutes = Math.floor((remainingTime % 3600000) / 60000);
    let seconds = Math.floor((remainingTime % 60000) / 1000);
    if (seconds < 10) {
        seconds = '0' + seconds;
    }
    return `${minutes == 0 ? '' : minutes + ':'} ${seconds}`;
};
const timeComparison = (currentTime, startTime, endTime) => {
    // 比较开始时间和结束时间与当前时间
    if (startTime < currentTime && currentTime < endTime) {
        return '使用中';
    } else if (currentTime < startTime) {
        return '未开始';
    } else {
        return '已完成';
    }
};
const getMinuteDifference = (startTime, endTime) => {
    const start = new Date(startTime).getTime();
    const end = new Date(endTime).getTime();
    const diffInMinutes = Math.abs(end - start) / 60000;
    return diffInMinutes;
};

//当前时间计时，结束停止
const reckonByTime = (num, num2, num3) => {
    var timerDisplay = '';
    var hours = num3;
    var minutes = num2;
    var seconds = num; // 增加秒数
    seconds++;

    // 检查是否需要增加分钟和小时
    if (seconds >= 60) {
        seconds = 0;
        minutes++;
    }
    if (minutes >= 60) {
        minutes = 0;
        hours++;
    }
    // 格式化时间
    timerDisplay = ('0' + hours).slice(-2) + ':' + ('0' + minutes).slice(-2) + ':' + ('0' + seconds).slice(-2);
    console.log(timerDisplay);
};

//比较当前时间和结束时间是否超出
const timeOut = (starttime, endtime) => {
    var currentTime = starttime;
    var endTime = endtime;
    // 判断当前时间是否已经超过结束时间
    if (currentTime > endTime) {
        return false;
    } else {
        return true;
    }
};

// 从现在时间计时 结束停止 格式为 00 ：00 ：00
const updateTimer = (startTime, endTime) => {
    // 开启定时器，每秒更新计时器
    // 获取当前时间的时间戳
    var currentTime = Date.now();

    // 计算时间差（以毫秒为单位）
    var timeDiff = currentTime - startTime;

    // 将时间差转换为小时、分钟和秒
    var hours = Math.floor(timeDiff / 3600000);
    var minutes = Math.floor((timeDiff % 3600000) / 60000);
    var seconds = Math.floor((timeDiff % 60000) / 1000);
    // 格式化时间为 "小时:分钟:秒"
    var formattedTime = ('0' + hours).slice(-2) + ':' + ('0' + minutes).slice(-2) + ':' + ('0' + seconds).slice(-2);
    return formattedTime;
};
const extractUrlParameters = (urlString) => {
    // 提取参数的正则表达式
    var regex = /[?&]([^=#]+)=([^&#]*)/g;

    // 存储参数的对象
    var params = {};
    var match;
    while ((match = regex.exec(decodeURIComponent(urlString)))) {
        var paramName = match[1]; // 参数名
        var paramValue = match[2]; // 参数值
        params[paramName] = paramValue; // 将参数存储到对象中
    }

    return params;
};
module.exports = {
    formatTime,
    formatDayTime,
    isDuringDate,
    calculateTimeDifferenceInSeconds,
    getCurrentDateTime,
    updateCountdown,
    timeComparison,
    getMinuteDifference,
    reckonByTime,
    timeOut,
    updateTimer,
    extractUrlParameters
};
